package com.android.happymall.fragment.detail;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.happymall.R;
import com.android.happymall.fragment.BaseLazyFragment;
import com.android.happymall.model.auction.Auction_detail_rule;
import com.android.happymall.model.auction.Auction_detail_ruleCallback;
import com.android.happymall.utils.ConstantsUrl;
import com.android.happymall.utils.HttpUtils;
import com.android.happymall.utils.MyLog;
import com.android.happymall.utils.ObjectUtils;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;


/**
 * 商品图文详情
 */
public class Fragment_Detail_GoodsDetail_DetailWeb extends BaseLazyFragment {

    @BindView(R.id.webView)
    WebView webView;
    @BindView(R.id.progress)
    RelativeLayout progress;
    @BindView(R.id.web_nodata)
    TextView nodata;
    private FragmentActivity mcontext;
    private View view;
    private static final String ARG_PARAM1 = "param1";
    private String mParam1;
    private String id;
    private boolean networkConnected;
    public Fragment_Detail_GoodsDetail_DetailWeb() {


    }

    public static Fragment_Detail_GoodsDetail_DetailWeb newInstance(String id) {
        Fragment_Detail_GoodsDetail_DetailWeb fragment = new Fragment_Detail_GoodsDetail_DetailWeb();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, id);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            id = getArguments().getString(ARG_PARAM1);
        }
    }

    @Override
    protected void initPrepare() {

    }

    @Override
    protected void onInvisible() {

    }


    @Override
    protected void initData() {
        getinitdata();

    }


    @Override
    protected View initView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_detail_goods_detail_web, container, false);
        mcontext = getActivity();
        ButterKnife.bind(this, view);

        return view;
    }
   private void getinitdata()
   {
       networkConnected = ObjectUtils.isNetworkConnected(mcontext);
       String address = ConstantsUrl.domain + ConstantsUrl.ACUTIONCONTENT;
       MyLog.e("地址", address);
       if (networkConnected) {
           HashMap<String, String> header = ObjectUtils.getHeader(mcontext);
           HashMap<String, String> map = new HashMap<>();
           map.put("id",id);
           HttpUtils.get(address, header, map, dataCallback);
       } else {
           ObjectUtils.toast(mcontext, "网络连接出现异常");
       }
   }

    Auction_detail_ruleCallback dataCallback= new Auction_detail_ruleCallback() {
        @Override
        public void onError(Call call, Exception e, int id) {
        }

        @Override
        public void onResponse(Auction_detail_rule response, int id) {
            if(response.getCode()==0) {
             //   ObjectUtils.GetDataNet(clickResetnetwork, progress,1);
                mParam1 = response.getData();
                initUI();
            }
            else
            {

            }
        }
    };


    private void initUI() {

        if (mParam1 == null || mParam1.equals("")) {
            progress.setVisibility(View.GONE);
            nodata.setVisibility(View.VISIBLE);
        } else {
            webView.setWebViewClient(new MyWebViewClient());
            webView.setWebChromeClient(new WebChromeClient() {
                @Override
                public void onProgressChanged(WebView view, int newProgress) {
                    super.onProgressChanged(view, newProgress);
                    if (newProgress == 100) {
                        progress.setVisibility(View.GONE);
                    }
                }
            });
            progress.setVisibility(View.GONE);
            nodata.setVisibility(View.GONE);
            //宽度适应屏幕
            String js = "<style>\n" +
                    "img{\n" +
                    " max-width:100%;\n" +
                    "height:auto\n" +
                    "}\n" +
                    "</style>" + mParam1;

            webView.loadDataWithBaseURL(null, js, "text/html", "utf-8", null);
        }


    }


    class MyWebViewClient extends WebViewClient {
        //拦截网址
    }


}
